package com.edu.edu.day25_homework.thread;

import android.os.AsyncTask;
import android.util.Log;

import com.edu.edu.day25_homework.Contans;
import com.edu.edu.day25_homework.bean.Movies;
import com.edu.edu.day25_homework.cache.SaveCache;
import com.edu.edu.day25_homework.utils.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/2.
 */
public class loadJsonTask extends AsyncTask<String,Void,List<Movies>>{


    private byte[] data;

    public interface onGetJsonResultListener{
        void onGetJsonResult(List<Movies> result);
    }
    private onGetJsonResultListener mListener;

    public  loadJsonTask(onGetJsonResultListener listener){
        this.mListener = listener;
    }


    @Override
    protected List<Movies> doInBackground(String... params) {
        if(SaveCache.isJsonExist(Contans.MOVIES_JSON_DIR)){//若存在，从本地取数据
            Log.w("TAG",">>>>>>>>>>>>来自于缓存");
            data = SaveCache.getJsonFromLocal(Contans.MOVIES_JSON_DIR);
        }else{//如果不存在，从网络加载，
            Log.w("TAG",">>>>>>>>>>>>来自于网络");
             data = HttpUtil.loadData(params[0]);
            SaveCache.saveJsonToLocal(Contans.MOVIES_JSON_DIR,data);
        }
        //取到json后解析
        List<Movies> movies = parseJson(data);
        return movies;
//        byte[] data = HttpUtil.loadData(params[0]);
//        try {
//            String json = new String(data);
//            JSONObject object = new JSONObject(json);
//            List<Movies> movies = new ArrayList<>();
//            JSONArray array = object.getJSONArray("list");
//            for (int i = 0; i < array.length(); i++) {
//                JSONObject obj = array.getJSONObject(i);
//                String name = obj.getString("name");
//                String highlight = obj.getString("highlight");
//                String grade = obj.getString("grade");
//                String logo = obj.getString("logo");
//                String actors = obj.getString("actors");
//                String mobilePreview = obj.getString("mobilePreview");
//                Movies movie = new Movies(logo,name,grade,mobilePreview,highlight,actors);
//                movies.add(movie);
//            }
//            return movies;
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        return null;
    }
    public List<Movies> parseJson(byte[] data){
        try {
            String json = new String(data);
            JSONObject object = new JSONObject(json);
            List<Movies> movies = new ArrayList<>();
            JSONArray array = object.getJSONArray("list");
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                String name = obj.getString("name");
                String highlight = obj.getString("highlight");
                String grade = obj.getString("grade");
                String logo = obj.getString("logo");
                String actors = obj.getString("actors");
                String mobilePreview = obj.getString("mobilePreview");
                Movies movie = new Movies(logo,name,grade,mobilePreview,highlight,actors);
                movies.add(movie);
            }
            return movies;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(List<Movies> movies) {
        super.onPostExecute(movies);
        if(mListener != null && movies != null){
            mListener.onGetJsonResult(movies);
        }
    }
}
